package servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import dao.HotelDao;
import dao.ViewDao;
import model.Hotel;
import model.View;
import util.GetWord;

/**
 * Servlet implementation class AddViewServlet
 */
@WebServlet("/UpdateHotelServlet")
public class UpdateHotelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateHotelServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		String email = "";
		String price = "0";
		String sum = "0";
		String info = "";
		String hname = "";
		String savePath = this.getServletContext().getRealPath("/hotel");
		File file = new File(savePath);
		if (!file.exists() && !file.isDirectory()) {
			System.out.println(savePath + "1434");
			file.mkdir();
		}
		String message = "";
		try {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setHeaderEncoding("UTF-8");
			if (!ServletFileUpload.isMultipartContent(request)) {
				return;
			}
			List<FileItem> list = upload.parseRequest(request);
			for (FileItem item : list) {
				if (item.isFormField()) {
					String name = item.getFieldName();
					String value = item.getString("UTF-8");
					System.out.println(name + " = " + value);
					if (name.equals("email")) {
						email = value;
					}
					if (name.equals("price")) {
						price = value;
					}
					if (name.equals("sum")) {
						sum = value;
					}
					if (name.equals("info")) {
						info = value;
					}
					if (name.equals("hname")) {
						hname = value;
					}
				} else {

					String filename = item.getName();
					System.out.println(filename);
					if (filename == null || filename.trim().equals("")) {
						continue;
					}
					filename = filename.substring(filename.lastIndexOf("\\") + 1);
				//	String word = GetWord.getPingYin(vname);
				//	System.out.println("null");
				//	System.out.println(info + price);
					filename = email + ".jpg";
					InputStream in = item.getInputStream();
					FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
					byte buffer[] = new byte[1044];
					int len = 0;
					while ((len = in.read(buffer)) > 0) {
						out.write(buffer, 0, len);
					}
					in.close();
					out.close();
					item.delete();
					
					if (HotelDao.getHotelNoCacheForBool(email)) {
						Hotel hotel = new Hotel();
						hotel.setEmail(email);
						hotel.setInfo(info);
						hotel.setLocation("hotel/" + email + ".jpg");
						hotel.setPrice(Integer.parseInt(price));
						hotel.setSum(Integer.parseInt(sum));
						hotel.setName(hname);
						HotelDao.updateHotel(hotel);
						String flag = "5";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminHotelList.jsp").forward(request, response);
					} else {
						System.out.println("n hace");
						String flag = "4";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminHotelList.jsp").forward(request, response);
					}

				}
			}
		} catch (

		Exception e) {
			System.out.println("ee");
			String flag = "4";
			request.setAttribute("flag", flag);
			request.getRequestDispatcher("/admin/AdminHotelList.jsp").forward(request, response);

		}

		// request.setAttribute("message",message);
		// request.getRequestDispatcher("/message.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
